﻿@inject NavigationManager Navigation
@inject IUserService userService
@inject SignInManager<AppIdentityUser> signInManager

<!-- WASM: https://stackoverflow.com/questions/37693516/unable-to-obtain-configuration-from-well-known-openid-configuration ? -->

@if( userService.RolesEnabled )
{
    <AuthorizeView>
        <Authorized>
            <div class="damselfly-statusbarpanel" title="Logged in as @context.User.Identity?.Name">
                <i class="processing-icon fas fa-user"/>
                <NavLink href="/config">@context.User.Identity?.Name</NavLink>
            </div>
            <div>
                <button class="btn btn-primary" onclick="location.href='/Identity/Account/Logout'" type="button">Log Out</button>
            </div>
        </Authorized>
        <NotAuthorized>
            <div>
                <button class="btn btn-primary" onclick="location.href='/Identity/Account/Login'" type="button">Log In</button>
            </div>
        </NotAuthorized>
    </AuthorizeView>
}

@code{

    /// <summary>
    ///     The below code would seem to be the right way to do this,
    ///     but not so, because of the issue here:
    ///     https://github.com/dotnet/aspnetcore/issues/13601
    ///     More discussion here.
    ///     https://github.com/dotnet/aspnetcore/issues/34095
    /// </summary>
    /// <returns></returns>
    async Task BeginSignOut()
    {
        await signInManager.SignOutAsync();
        var key = Guid.NewGuid();
        Navigation.NavigateTo($"/Identity/Account/Logout?key={key}", true);
    }

}